package com.hyt.it.ogt.ykcj.service.grade.impl;

import com.hyt.it.ogt.ykcj.common.annotation.ChangedExam;
import com.hyt.it.ogt.ykcj.common.constant.Constants;
import com.hyt.it.ogt.ykcj.common.enums.BizErrorCode;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.common.utils.*;
import com.hyt.it.ogt.ykcj.common.utils.bean.BeanUtils;
import com.hyt.it.ogt.ykcj.domain.exam.*;
import com.hyt.it.ogt.ykcj.domain.grade.ServantExamineeInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ServantExamineeSubjectGrade;
import com.hyt.it.ogt.ykcj.domain.vo.*;
import com.hyt.it.ogt.ykcj.mapper.exam.*;
import com.hyt.it.ogt.ykcj.mapper.grade.ServantExamineeInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.ServantExamineeSubjectGradeMapper;
import com.hyt.it.ogt.ykcj.service.exam.IChangedExamInfoService;
import com.hyt.it.ogt.ykcj.service.grade.ExportUtils;
import com.hyt.it.ogt.ykcj.service.grade.IServantExamineeGradeImportService;
import com.hyt.it.ogt.ykcj.service.grade.IServantExamineeInfoService;
import com.hyt.it.ogt.ykcj.service.system.ISysDictDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.Collator;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 公务员考生信息Service业务层处理
 *
 * @author admin
 * @date 2021-09-09
 */
@Service
public class ServantExamineeInfoServiceImpl implements IServantExamineeInfoService {
    private static final Logger log = LoggerFactory.getLogger(ServantExamineeInfoServiceImpl.class);

    @Autowired
    private ServantExamineeInfoMapper servantExamineeInfoMapper;
    @Autowired
    private ServantExamineeSubjectGradeMapper servantExamineeSubjectGradeMapper;

    @Autowired
    private ExamPostInfoMapper postInfoMapper;

    @Autowired
    private ExamPostScoreLineSettingSubMapper settingSubMapper;

    @Autowired
    private ExamPostScoreLineSettingMapper settingMapper;

    @Autowired
    private ExamInfoMapper examInfoMapper;

    @Autowired
    private ExamSubjectInfoMapper examSubjectInfoMapper;

    @Autowired
    private ExamStemInfoMapper examStemInfoMapper;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ChangedExamInfoMapper changedExamInfoMapper;

    @Autowired
    private ExamPostInfoMapper examPostInfoMapper;

    @Autowired
    private IChangedExamInfoService changedExamInfoService;

    /**
     * 查询公务员考生信息
     *
     * @param id 公务员考生信息ID
     * @return 公务员考生信息
     */
    @Override
    public ServantExamineeInfo selectServantExamineeInfoById(String id) {
        return servantExamineeInfoMapper.selectServantExamineeInfoById(id);
    }

    /**
     * 查询公务员考生信息列表
     *
     * @param servantExamineeInfo 公务员考生信息
     * @return 公务员考生信息
     */
    @Override
    public List<ServantExamineeInfo> selectServantExamineeInfoList(ServantExamineeInfo servantExamineeInfo) {
        return servantExamineeInfoMapper.selectServantExamineeInfoList(servantExamineeInfo);
    }

    /**
     * 新增公务员考生信息
     *
     * @param servantExamineeInfo 公务员考生信息
     * @return 结果
     */
    @Override
    public int insertServantExamineeInfo(ServantExamineeInfo servantExamineeInfo) {
        servantExamineeInfo.setCreateTime(DateUtils.getNowDate());
        Constants.changedExamId.set(servantExamineeInfo.getExamId());
        return servantExamineeInfoMapper.insertServantExamineeInfo(servantExamineeInfo);
    }

    /**
     * 修改公务员考生信息
     *
     * @param servantExamineeInfo 公务员考生信息
     * @return 结果
     */
    @Override
    public int updateServantExamineeInfo(ServantExamineeInfo servantExamineeInfo) {
        servantExamineeInfo.setUpdateTime(DateUtils.getNowDate());
        //添加考试变更信息
        changedExamInfoService.addChangedExamInfo(servantExamineeInfo.getExamId());
        return servantExamineeInfoMapper.updateServantExamineeInfo(servantExamineeInfo);
    }

    /**
     * 批量删除公务员考生信息
     *
     * @param ids 需要删除的公务员考生信息ID
     * @return 结果
     */
    @Override
    public int deleteServantExamineeInfoByIds(String[] ids) {
        //添加考试变更信息
        ServantExamineeInfo servantExamineeInfo = servantExamineeInfoMapper.selectServantExamineeInfoById(ids[0]);
        changedExamInfoService.addChangedExamInfo(servantExamineeInfo.getExamId());
        return servantExamineeInfoMapper.deleteServantExamineeInfoByIds(ids);
    }

    /**
     * 删除公务员考生信息信息
     *
     * @param id 公务员考生信息ID
     * @return 结果
     */
    @Override
    public int deleteServantExamineeInfoById(String id) {
        //添加考试变更信息
        ServantExamineeInfo servantExamineeInfo = servantExamineeInfoMapper.selectServantExamineeInfoById(id);
        changedExamInfoService.addChangedExamInfo(servantExamineeInfo.getExamId());
        return servantExamineeInfoMapper.deleteServantExamineeInfoById(id);
    }

    @Override
    public Integer deleteExamineeGradeById(String id, String subjectId) {
        String isDel = "D"; // 是否考生信息（D：删除，N：不删）
        if (servantExamineeSubjectGradeMapper.getIsDelExamineeInfoStatus(id, subjectId) > 0) {
            isDel = null;
        }
        //添加考试变更信息
        ServantExamineeInfo servantExamineeInfo = servantExamineeInfoMapper.selectServantExamineeInfoById(id);
        changedExamInfoService.addChangedExamInfo(servantExamineeInfo.getExamId());
        return servantExamineeInfoMapper.deleteExamineeGradeById(id, subjectId, isDel);
    }

    @Override
    public List<ServantExamineeInfoVo> selectExamineeGradePage(ServantExamineeInfoVo servantExamineeInfoVo) {
        List<ServantExamineeInfoVo> list = servantExamineeInfoMapper.selectExamineeGradePage(servantExamineeInfoVo);
        if (StringUtils.isEmpty(servantExamineeInfoVo.getSubjectId())) {
            for (ServantExamineeInfoVo infoVo : list) {
                ServantExamineeSubjectGradeVo grade = new ServantExamineeSubjectGradeVo();
                List<ServantExamineeSubjectGradeVo> gradeList = infoVo.getGradeList();
                for (ServantExamineeSubjectGradeVo gradeVo : gradeList) {
                    if ("1".equals(gradeVo.getIsComposite())) {
                        if (null == grade.getSubjectId()) {
                            grade.setSubjectId(gradeVo.getSubjectId());
                            grade.setScore(gradeVo.getScore());
                        } else {
                            grade.setSubjectId(grade.getSubjectId() + "+" + gradeVo.getSubjectId());
                            grade.setScore(grade.getScore().add(gradeVo.getScore()));
                        }
                    }
                }
                if (StringUtils.isNotEmpty(grade.getSubjectId()) && grade.getSubjectId().indexOf("+") != -1) {
                    gradeList.add(grade);
                }
            }
        }
        return list;
    }

    /**
     * 获取公务员考生成绩信息详细
     *
     * @param id 公务员考生信息ID
     * @return 公务员生成绩信息详细
     */
    @Override
    public ServantExamineeInfoVo selectExamineeGradeById(String id) {
        ServantExamineeInfoVo servantExamineeInfoVo = new ServantExamineeInfoVo();
        ServantExamineeInfo servantExamineeInfo = servantExamineeInfoMapper.selectServantExamineeInfoById(id);
        BeanUtils.copyBeanProp(servantExamineeInfoVo, servantExamineeInfo);
        ServantExamineeSubjectGrade servantExamineeSubjectGrade = new ServantExamineeSubjectGrade();
        servantExamineeSubjectGrade.setExamineeId(servantExamineeInfo.getId());
        List<ServantExamineeSubjectGrade> gradeList = servantExamineeSubjectGradeMapper.selectServantExamineeSubjectGradeList(servantExamineeSubjectGrade);
        List<ServantExamineeSubjectGradeVo> gradeVoList = new ArrayList<>();
        for (ServantExamineeSubjectGrade grade : gradeList) {
            ServantExamineeSubjectGradeVo gradeVo = new ServantExamineeSubjectGradeVo();
            BeanUtils.copyBeanProp(gradeVo, grade);
            gradeVoList.add(gradeVo);
        }
        servantExamineeInfoVo.setGradeList(gradeVoList);
        return servantExamineeInfoVo;
    }

    @Override
    @ChangedExam
    @Transactional
    public Integer saveExamineeGrade(ServantExamineeInfoVo servantExamineeInfoVo) throws Exception {
        this.checkExamineeGrade(servantExamineeInfoVo); // 提交数据检查
        Set<String> gradeSet = new TreeSet<>();
        ServantExamineeInfo servantExamineeInfo = new ServantExamineeInfo();
        BeanUtils.copyBeanProp(servantExamineeInfo, servantExamineeInfoVo);
        ExamPostInfo examPostInfo = new ExamPostInfo();
        examPostInfo.setExamId(servantExamineeInfo.getExamId());
        examPostInfo.setZwdm(servantExamineeInfo.getBkzwdm());
        List<ExamPostInfo> examPostInfoList = examPostInfoMapper.selectExamPostInfoList(examPostInfo);
        if (CollectionUtils.isEmpty(examPostInfoList)) {
            throw new CustomException("报考职位编号 " + servantExamineeInfo.getBkzwdm() + " 错误");
        }
        Map<String, String> examPostInfoMap = examPostInfoList.stream().collect(Collectors.toMap(ExamPostInfo::getZwdm, ExamPostInfo::getDwdm));
        servantExamineeInfo.setBkdwdm(examPostInfoMap.get(servantExamineeInfo.getBkzwdm()));
        int ret;
        Boolean isSingleSubject = false; // 是否单科（默认为:否）
        if (StringUtils.isNotEmpty(servantExamineeInfo.getId())) { // 更新
            servantExamineeInfo.setArchivedStatus("N");
            servantExamineeInfo.updateInfo(SecurityUtils.getUsername());
            List<ServantExamineeSubjectGrade> insertGradeList = new ArrayList<>();
            List<ServantExamineeSubjectGrade> updateGradeList = new ArrayList<>();
            List<String> deleteGradeList = new ArrayList<>();
            for (ServantExamineeSubjectGradeVo gradeVo : servantExamineeInfoVo.getGradeList()) {
                if (gradeSet.contains(gradeVo.getSubjectId() + gradeVo.getStemId())) {
                    throw new CustomException(BizErrorCode.COM_E0005);
                } else {
                    gradeSet.add(gradeVo.getSubjectId() + gradeVo.getStemId());
                }
                ServantExamineeSubjectGrade grade = new ServantExamineeSubjectGrade();
                BeanUtils.copyBeanProp(grade, gradeVo);
                grade.setExamId(servantExamineeInfo.getExamId());
                grade.setExamineeId(servantExamineeInfo.getId());
                if (StringUtils.isEmpty(grade.getStemId())) {
                    grade.setStemId("");
                } else {
                    isSingleSubject = true;
                }
                grade.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
                grade.updateInfo(SecurityUtils.getUsername());
                if (StringUtils.isEmpty(grade.getId()) && !"D".equals(grade.getDelFlag())) {
                    grade.setId(IdUtils.generateId());
                    grade.createInfo(SecurityUtils.getUsername());
                    insertGradeList.add(grade);
                } else if (StringUtils.isNotEmpty(grade.getId()) && !"D".equals(grade.getDelFlag())) {
                    insertGradeList.add(grade);
                    deleteGradeList.add(grade.getId());
                } else if (StringUtils.isNotEmpty(grade.getId()) && "D".equals(grade.getDelFlag())) {
                    deleteGradeList.add(grade.getId());
                }
            }
            // 单科成绩导入的总分处理
            if (isSingleSubject) { // 题型分数合并成单科总分
                List<ServantExamineeSubjectGrade> gradeList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(updateGradeList)) {
                    gradeList.addAll(updateGradeList);
                }
                if (!CollectionUtils.isEmpty(insertGradeList)) {
                    gradeList.addAll(insertGradeList);
                }
                ServantExamineeSubjectGrade grade = new ServantExamineeSubjectGrade();
                getSubjectGrade(gradeList, grade);
                ServantExamineeSubjectGrade gradeParam = new ServantExamineeSubjectGrade();
                gradeParam.setExamId(grade.getExamId());
                gradeParam.setExamineeId(grade.getExamineeId());
                gradeParam.setSubjectId(grade.getSubjectId());
                gradeParam.setStemId(grade.getStemId());
                List<ServantExamineeSubjectGrade> subjectGrade = servantExamineeSubjectGradeMapper.selectServantExamineeSubjectGradeList(gradeParam);
                if (!CollectionUtils.isEmpty(subjectGrade)) {
                    grade.setId(subjectGrade.get(0).getId());
                    updateGradeList.add(grade);
                } else {
                    insertGradeList.add(grade);
                }
            }
            try {
                if (!CollectionUtils.isEmpty(deleteGradeList)) {
                    servantExamineeSubjectGradeMapper.deleteServantExamineeSubjectGradeByIds(deleteGradeList.toArray(new String[deleteGradeList.size()]));
                }
                if (!CollectionUtils.isEmpty(updateGradeList)) {
                    servantExamineeSubjectGradeMapper.updateBatchServantExamineeSubjectGrade(updateGradeList);
//                    servantExamineeSubjectGradeMapper.insertBatchServantExamineeSubjectGrade(updateGradeList);
                }
                if (!CollectionUtils.isEmpty(insertGradeList)) {
                    servantExamineeSubjectGradeMapper.insertBatchServantExamineeSubjectGrade(insertGradeList);
                }
                ret = servantExamineeInfoMapper.updateServantExamineeInfo(servantExamineeInfo);
            } catch (DuplicateKeyException e) { // SQLIntegrityConstraintViolationException
                throw new CustomException(BizErrorCode.COM_E0005);
            }
        } else { // 新增
            ServantExamineeInfo servantExamineeInfoParams = new ServantExamineeInfo();
            servantExamineeInfoParams.setExamId(servantExamineeInfo.getExamId());
            servantExamineeInfoParams.setIdcard(servantExamineeInfo.getIdcard());
            List<ServantExamineeInfo> existingServantExamineeInfolist = servantExamineeInfoMapper.selectServantExamineeInfoList(servantExamineeInfoParams);
            if (!CollectionUtils.isEmpty(existingServantExamineeInfolist)) { // 考生信息已存在则用已有信息
//                BeanUtils.copyBeanProp(servantExamineeInfo, existingServantExamineeInfolist.get(0));
                servantExamineeInfo.setId(existingServantExamineeInfolist.get(0).getId());
                servantExamineeInfo.setArchivedStatus("N");
            } else {
                servantExamineeInfo.setId(IdUtils.generateId());
                servantExamineeInfo.createInfo(SecurityUtils.getUsername());
            }
            servantExamineeInfo.updateInfo(SecurityUtils.getUsername());
            List<ServantExamineeSubjectGrade> gradeList = new ArrayList<>();
            for (ServantExamineeSubjectGradeVo gradeVo : servantExamineeInfoVo.getGradeList()) {
                if (gradeSet.contains(gradeVo.getSubjectId() + gradeVo.getStemId())) {
                    throw new CustomException(BizErrorCode.COM_E0005);
                } else {
                    gradeSet.add(gradeVo.getSubjectId() + gradeVo.getStemId());
                }
                ServantExamineeSubjectGrade grade = new ServantExamineeSubjectGrade();
                BeanUtils.copyBeanProp(grade, gradeVo);
                grade.setId(IdUtils.generateId());
                grade.setExamId(servantExamineeInfo.getExamId());
                grade.setExamineeId(servantExamineeInfo.getId());
                if (StringUtils.isEmpty(grade.getStemId())) {
                    grade.setStemId("");
                } else {
                    isSingleSubject = true;
                }
                grade.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
                grade.createInfo(SecurityUtils.getUsername());
                grade.updateInfo(SecurityUtils.getUsername());
                gradeList.add(grade);
            }
            // 单科成绩导入的总分处理
            if (isSingleSubject) { // 题型分数合并成单科总分
                ServantExamineeSubjectGrade grade = new ServantExamineeSubjectGrade();
                getSubjectGrade(gradeList, grade);
                ServantExamineeSubjectGrade gradeParam = new ServantExamineeSubjectGrade();
                gradeParam.setExamId(grade.getExamId());
                gradeParam.setExamineeId(grade.getExamineeId());
                gradeParam.setSubjectId(grade.getSubjectId());
                gradeParam.setStemId(grade.getStemId());
                List<ServantExamineeSubjectGrade> subjectGrade = servantExamineeSubjectGradeMapper.selectServantExamineeSubjectGradeList(gradeParam);
                if (!CollectionUtils.isEmpty(subjectGrade)) {
                    grade.setId(subjectGrade.get(0).getId());
                    servantExamineeSubjectGradeMapper.updateServantExamineeSubjectGrade(grade);
                } else {
                    gradeList.add(grade);
                }
            }
            try {
                servantExamineeSubjectGradeMapper.insertBatchServantExamineeSubjectGrade(gradeList);
                ret = servantExamineeInfoMapper.insertServantExamineeInfo(servantExamineeInfo);
            } catch (DuplicateKeyException e) { // SQLIntegrityConstraintViolationException
                throw new CustomException(BizErrorCode.COM_E0005);
            }
        }
        // 插入需要变更的考试信息
        changedExamInfoService.addChangedExamInfo(servantExamineeInfoVo.getExamId());
        return ret;
    }

    /**
     * 题型分数合并成单科总分
     * @param gradeList 题型分数集
     * @param grade 合并单科分数
     */
    public void getSubjectGrade(List<ServantExamineeSubjectGrade> gradeList, ServantExamineeSubjectGrade grade) {
        BigDecimal score = new BigDecimal(0);
        for (ServantExamineeSubjectGrade stem : gradeList) {
            score = score.add(stem.getScore());
        }
        BeanUtils.copyBeanProp(grade, gradeList.get(0));
        grade.setId(IdUtils.generateId());
        grade.setStemId("");
        grade.setScore(score);
    }

    /**
     * 公务员考生成绩信息提交检查
     * @param servantExamineeInfoVo 公务员考生成绩信息
     * @throws Exception
     */
    private void checkExamineeGrade(ServantExamineeInfoVo servantExamineeInfoVo) throws Exception {
        if (StringUtils.isEmpty(servantExamineeInfoVo.getExamId())) {
            throw new CustomException("考试ID不能为空");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getName())) {
            throw new CustomException("考生姓名不能为空");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getAdmissionNo())) {
            throw new CustomException("准考证号不能为空");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getIdcard())) {
            throw new CustomException("身份证号不能为空");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getSex())) {
            throw new CustomException("性别未选择");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getBkzwdm())) {
            throw new CustomException("报考职位编号不能为空");
        }
        if (StringUtils.isEmpty(servantExamineeInfoVo.getBkdwdm())) {
            throw new CustomException("报考部门编号不能为空");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("examInfoId", servantExamineeInfoVo.getExamId());
        List<ExamSubjectInfo> examSubjectInfoList = examSubjectInfoMapper.selectExamSubjectInfoListAllByParms(params);
        Map<String, ExamSubjectInfo> examSubjectInfoMap = examSubjectInfoList.stream().collect(Collectors.toMap(ExamSubjectInfo::getId, ExamSubjectInfo -> ExamSubjectInfo));
        BigDecimal score = new BigDecimal(0);
        for (ServantExamineeSubjectGradeVo gradeVo : servantExamineeInfoVo.getGradeList()) {
            if (StringUtils.isNotEmpty(gradeVo.getDelFlag()) && "N".equals(gradeVo.getDelFlag())) {
                if (StringUtils.isEmpty(gradeVo.getSubjectId())) {
                    throw new CustomException("科目未选择");
                }
                if (StringUtils.isEmpty(gradeVo.getScore().toString())) {
                    throw new CustomException("成绩不能为空");
                } else {
                    ExamSubjectInfo subjectInfo = examSubjectInfoMap.get(gradeVo.getSubjectId());
                    if (StringUtils.isEmpty(gradeVo.getStemId())) {
                        if (gradeVo.getScore().compareTo(subjectInfo.getPaperFullMarks()) == 1) {
                            throw new CustomException(subjectInfo.getExamSubjectName() + "成绩不能大于试卷满分的" + subjectInfo.getPaperFullMarks() + "分");
                        }
                    } else {
                        score = score.add(gradeVo.getScore());
                        if (score.compareTo(subjectInfo.getPaperFullMarks()) > 0) {
                            throw new CustomException(subjectInfo.getExamSubjectName() + "成绩不能大于试卷满分的" + subjectInfo.getPaperFullMarks() + "分");
                        }
                    }
                }
            }
        }
    }

    @Override
    @ChangedExam
    @Transactional
    public Integer updateArchiveByIds(String archivedStatus, Date archivedDate, String examId, String ids) {
        Integer integer;
        if (StringUtils.isNotEmpty(ids) && ids.contains(",")) {
            integer = servantExamineeInfoMapper.updateArchiveByIds(archivedStatus, archivedDate, examId, ids.split(","));
        } else {
            integer = servantExamineeInfoMapper.updateArchiveById(archivedStatus, archivedDate, examId, ids);
        }
        //Constants.changedExamId.set(examId);
        return integer;
    }

    @Override
    public void insertBatchExamineeGrade(List<ServantExamineeGradeImportVo> list) {
        int maxImport = IServantExamineeGradeImportService.MAX_IMPORT;
        log.info("插入成绩信息Begin...总共插入{}条数据,每次插入{}条成绩信息", list.size(), maxImport);
        //
        int insertNum =1;
        // 总共需要插入次数
        int totalInsertTimes = list.size() / IServantExamineeGradeImportService.MAX_IMPORT + 1;
        log.info("插入成绩信息,总共需要插入{}次", totalInsertTimes);
        // 需要插入的成绩信息和科目成绩信息
        List<ServantExamineeInfo> insertExamineeInfoList = new ArrayList<>();
        List<ServantExamineeSubjectGrade> insertExamineeSubjectGradeList = new ArrayList<>();
        // 当前插入次数
        int curInsertTime = 1;
        for (ServantExamineeGradeImportVo examineeGradeVo : list) {
            insertExamineeInfoList.add(examineeGradeVo.getServantExamineeInfo());
            insertExamineeSubjectGradeList.addAll(examineeGradeVo.getServantExamineeGradeList());

            if (insertNum == maxImport) {
                // 执行插入
                servantExamineeInfoMapper.insertBatchServantExamineeInfo(insertExamineeInfoList);
                servantExamineeSubjectGradeMapper.insertOrUpdateBatchServantExamineeSubjectGrade(insertExamineeSubjectGradeList);
                insertExamineeInfoList.clear();
                insertExamineeSubjectGradeList.clear();
                insertNum = 0;
                curInsertTime++;
            } else {
                // 最后一次插入数据如果小于200条也插入
                if (((curInsertTime - 1) * maxImport + insertNum) == list.size()) {
                    servantExamineeInfoMapper.insertBatchServantExamineeInfo(insertExamineeInfoList);
                    servantExamineeSubjectGradeMapper.insertOrUpdateBatchServantExamineeSubjectGrade(insertExamineeSubjectGradeList);
                }
            }
            insertNum++;
        }
        log.info("插入成绩信息End...");
    }

    @Override
    public String downloadExamineeGradeList(ServantExamineeInfoVo servantExamineeInfoVo, String isTemplate) {
        String fileName; // 文件名称
        String sheetName; // 工作表名称
        int pageSize = Integer.MAX_VALUE; // 单页最大行数
        List<String> titles = new ArrayList<>(Arrays.asList(new String[]{"准考试号", "考生姓名", "性别", "身份证号", "报考职位编号", "报考部门编号"}));
        List<String> keys = new ArrayList<>(Arrays.asList(new String[]{"admissionNo", "name", "sex", "idcard", "bkzwdm", "bkdwdm"}));
        List<Map<String, String>> recordList = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(servantExamineeInfoVo.getSubjectId())) { // 单科成绩表
            fileName = ("Y".equals(isTemplate) ? "公务员单科成绩导入模板":"公务员单科成绩");
            sheetName = "单科成绩";
            params.put("examInfoId", servantExamineeInfoVo.getExamId());
            params.put("id", servantExamineeInfoVo.getSubjectId());
            List<ExamSubjectInfo> subjectInfoList = examSubjectInfoMapper.selectExamSubjectInfoListAllByParms(params);
            titles.add("考试科目(" + subjectInfoList.get(0).getExamSubjectName() + ")");
            ExamStemInfo examStemInfo = new ExamStemInfo();
            examStemInfo.setSubjectId(servantExamineeInfoVo.getSubjectId());
            params = new HashMap<>();
            params.put("orderBy", "sort");
            examStemInfo.setParams(params);
            List<ExamStemInfo> stemInfoList = examStemInfoMapper.selectExamStemInfoList(examStemInfo);
            for (ExamStemInfo stemInfo : stemInfoList) {
                titles.add(stemInfo.getName());
            }
        } else { // 总成绩表
            fileName = ("Y".equals(isTemplate) ? "公务员总成绩导入模板":"公务员总成绩");
            sheetName = "总成绩";
            params.put("examInfoId", servantExamineeInfoVo.getExamId());
            List<ExamSubjectInfo> subjectInfoList = examSubjectInfoMapper.selectExamSubjectInfoListAllByParms(params);
            for (ExamSubjectInfo subjectInfo : subjectInfoList) {
                titles.add(subjectInfo.getExamSubjectName());
            }
        }
        if ("N".equals(isTemplate)) {
            List<ServantExamineeInfoVo> datalist = servantExamineeInfoMapper.selectExamineeGradeList(servantExamineeInfoVo);
            this.convertExamineeGrade(keys, recordList, datalist, servantExamineeInfoVo.getSubjectId());
        }
        return ExportUtils.exportExcelTemplate(fileName, sheetName, pageSize, titles, keys, recordList);
    }

    public void convertExamineeGrade(List<String> keys, List<Map<String, String>> recordList, List<ServantExamineeInfoVo> datalist, String subjectId) {
        Set<String> keySet = new TreeSet<>();
        Map<String, String> infoMap;
        Map<String, String> gradeMap;
        Map<String, String> sexDataMap = dictDataService.selectDictDataMap("sys_user_sex", "K");
        for (ServantExamineeInfoVo vo : datalist) {
            vo.setSex(sexDataMap.get(vo.getSex()));
            infoMap = vo.toMap();
            gradeMap = new HashMap<>();
            for (ServantExamineeSubjectGradeVo grade : vo.getGradeList()) {
                if (StringUtils.isNotEmpty(subjectId)) { // 单科成绩表
                    keySet.add(grade.getSubjectId());
                    gradeMap.put(grade.getSubjectId(), grade.getSubjectName());
                    keySet.add(grade.getStemId());
                    gradeMap.put(grade.getStemId(), new DecimalFormat("0.00").format(grade.getScore()));
                } else { // 总成绩表
                    keySet.add(grade.getSubjectId());
                    gradeMap.put(grade.getSubjectId(), new DecimalFormat("0.00").format(grade.getScore()));
                }
            }
            infoMap.putAll(gradeMap);
            recordList.add(infoMap);
        }
        keys.addAll(new ArrayList<>(keySet));
    }

    @Override
    public ExamScoreLineRsp queryExamScoreLine(ExamScoreLineReq examScoreLineReq) {
//        if(com.hyt.common.utils.StringUtils.isEmpty(examScoreLineReq.getExamYear())){
//            throw  new CustomException("examYear is null");
//        }
        // 1、查询考试招考职位部门信息 2、查询分数线信息 3、查询历年考试信息
        log.info("查询成绩档按-考试分数线信息");
        ExamScoreLineRsp rsp = new ExamScoreLineRsp();
        rsp.setExamPostNameList(queryExamPostNameList(examScoreLineReq));
        rsp.setPostScoreInfoVo(queryPostScoreInfo(examScoreLineReq));
        rsp.setExamList(queryHistoryScoreLine(examScoreLineReq));
        return rsp;
    }

    /**
     * 查询机构部门列表
     *
     * @param examScoreLineReq
     * @return
     */
    private List<UnitPostVo> queryExamPostNameList(ExamScoreLineReq examScoreLineReq) {
        log.info("查询机构下的单位信息");
        List<UnitPostVo> examPostNameList = postInfoMapper.selectExamPostNameList(examScoreLineReq);
        Comparator comparator = Collator.getInstance(Locale.CHINA);
        Collections.sort(examPostNameList,((o1, o2) -> comparator.compare(o1.getDwmc(),o2.getDwmc())));
        return examPostNameList;
    }

    /**
     * 查询招考职位人数和分数相关
     *
     * @param examScoreLineReq
     * @return
     */
    private PostScoreInfoVo queryPostScoreInfo(ExamScoreLineReq examScoreLineReq) {
        log.info("查询招考职位、人数分数线相关信息");
        PostScoreInfoVo postScoreInfoVo = postInfoMapper.selectPostNumAndRequiredNum(examScoreLineReq); // 获取 职位个数、招录人数
        PostScoreInfoVo postScoreInfoVo2 = settingSubMapper.getAvgAndMinScoreLine(examScoreLineReq); // 获取 职位平均分数线、最低分数线
        postScoreInfoVo.setAvgScore(postScoreInfoVo2.getAvgScore()); // 平均分数线
        postScoreInfoVo.setMinScore(postScoreInfoVo2.getMinScore()); // 最低分数线
//        // 查找总复合分数线和
//        String totalScore = settingSubMapper.countTotalScore(examScoreLineReq);
//        if (StringUtils.isNotEmpty(totalScore) && postScoreInfoVo.getPostNum() > 0) {
//            BigDecimal avgScore = new BigDecimal(totalScore).divide(BigDecimal.valueOf(postScoreInfoVo.getPostNum()), 1, BigDecimal.ROUND_HALF_UP);
//            // 平均分数线=面试考生最低分数和/职位个数
//            postScoreInfoVo.setAvgScore(avgScore);
//        }
        // 查找最低分数线  最低分数线指面试名单中最低复合分数
        String minSettingScore = settingSubMapper.selectMinSettingScore(examScoreLineReq);
        if(null != minSettingScore) {
            BigDecimal bd = new BigDecimal(minSettingScore);
            minSettingScore = bd.setScale(1, RoundingMode.HALF_UP).toString();
        }
        postScoreInfoVo.setMinScore(minSettingScore);
        return postScoreInfoVo;
    }

    /**
     * 查询历史考试信息
     *
     * @param examScoreLineReq
     * @return
     */
    private List<HistoryScoreLineVo> queryHistoryScoreLine(ExamScoreLineReq examScoreLineReq) {
        log.info("查询历史分数线");
        List<HistoryScoreLineVo> list = examInfoMapper.queryHistoryServantExam(examScoreLineReq.getExamYear(), Constants.EXAM_TYPE_1);
        return list;
    }


    /**
     * 查询单位下的职位信息
     */
    @Override
    public List<UnitPostVo> queryUnitPostInfo(ExamScoreLineReq examScoreLineReq) {
        log.info("查询单位职位分数线");
        //获取职位
        List<UnitPostVo> unitPostVos = queryExamPostNameList(examScoreLineReq);
        //获取职位分数线和面试人数
        for (UnitPostVo unitPostVo : unitPostVos) {
            examScoreLineReq.setZwdm(unitPostVo.getZwdm());
            ServantExamineeInfo servantExamineeInfo = new ServantExamineeInfo();
            servantExamineeInfo.setExamId(examScoreLineReq.getExamId());
            servantExamineeInfo.setBkzwdm(unitPostVo.getZwdm());
            servantExamineeInfo.setInterviewStatus("Y");
            unitPostVo.setMsrs(servantExamineeInfoMapper.selectServantExamineeInfoCount(servantExamineeInfo).toString()); // 获得面试人数
            String score = settingSubMapper.selectMinSettingScore(examScoreLineReq);
            if (null != score)
                score = new BigDecimal(score).setScale(1, BigDecimal.ROUND_HALF_UP).toString();
            unitPostVo.setScore(score);
        }
        Comparator comparator = Collator.getInstance(Locale.CHINA);
        Collections.sort(unitPostVos,((o1, o2) -> comparator.compare(o1.getZwmc(),o2.getZwmc())));
        return unitPostVos;
    }

    /**
     * 查询单位下分数线信息
     */
    @Override
    public PostScoreInfoVo queryUnitScoreLine(ExamScoreLineReq examScoreLineReq) {
        log.info("查询单位职位分数线");
        PostScoreInfoVo postScoreInfoVo = queryPostScoreInfo(examScoreLineReq);
        return postScoreInfoVo;
    }

    /**
     * 获取面试名单
     *
     * @param examScoreLineReq
     * @return
     */
    @Override
    public InterViewRsp getInterViewList(ExamScoreLineReq examScoreLineReq) {
        InterViewRsp interViewRsp = new InterViewRsp();
        //职位信息
        interViewRsp.setExamPostInfo(postInfoMapper.selectPostInfoByExamIdAndZwdm(examScoreLineReq.getExamId(), examScoreLineReq.getZwdm()));
        //面试名单
        Map<String, String> sexDataMap = dictDataService.selectDictDataMap("sys_user_sex", "K");
        List<InterviewVo> list = new ArrayList<>();
        ServantExamineeInfo servantExamineeInfo = new ServantExamineeInfo();
        servantExamineeInfo.setExamId(examScoreLineReq.getExamId());
        servantExamineeInfo.setBkzwdm(examScoreLineReq.getZwdm());
        servantExamineeInfo.setBkdwdm(examScoreLineReq.getDwdm());
        servantExamineeInfo.setInterviewStatus("Y");
        List<ServantExamineeInfo> list2 = servantExamineeInfoMapper.selectServantExamineeInfoList(servantExamineeInfo); // 获取面试考生列表
        for (ServantExamineeInfo examineeInfo : list2) {
            InterviewVo interviewVo = new InterviewVo();
            BeanUtils.copyBeanProp(interviewVo, examineeInfo);
            interviewVo.setSex(sexDataMap.get(interviewVo.getSex()));
            list.add(interviewVo);
        }
        interViewRsp.setList(list);
        return interViewRsp;
    }

    /**
     * 获取机构性质
     *
     * @param examId
     * @return
     */
    @Override
    public List<JgxzVo> getJgxzList(String examId) {
        List<JgxzVo> jgxzVos = postInfoMapper.getJgxzList(examId);
        for (JgxzVo jgxzVo : jgxzVos) {
            jgxzVo.setJgxzLabel(DictUtils.getDictLabel("exam_organ_nature", jgxzVo.getJgxz()));
        }
        return jgxzVos;
    }

    /**
     * 查看排名
     *
     * @param examScoreLineReq
     * @return
     */
    @Override
    public List<InterviewRank> getInterviewRankList(ExamScoreLineReq examScoreLineReq, Integer rankingBaseNum) {
        //获取考生成绩信息
        List<InterviewRank> interviewRanks = servantExamineeInfoMapper.queryRankGradeList(examScoreLineReq);

        //排名
        int index = 0;
        //上一名的分数
        double lastScore = -1;
        for (InterviewRank interviewRank : interviewRanks)
        {
            if(Double.compare(interviewRank.getScore().doubleValue(),lastScore) != 0)
            {
                lastScore = interviewRank.getScore().doubleValue();
                index++;
            }
            interviewRank.setRanking(index + rankingBaseNum);
        }
        return interviewRanks;
    }


    /**
     * 获取历史考试列表
     *
     * @param historyExamReq
     * @return
     */

    @Override
    public List<HistoryScoreLineVo> getHistoryExamNameList(HistoryExamReq historyExamReq) {
        return servantExamineeInfoMapper.getHistoryExamNameList(historyExamReq);
    }

    /**
     * 获取职位上方职位信息
     *
     * @param examScoreLineReq
     * @return
     */
    @Override
    public ExamPostInfo getPostInfo(ExamScoreLineReq examScoreLineReq) {
        return postInfoMapper.selectPostInfoByExamIdAndZwdm(examScoreLineReq.getExamId(), examScoreLineReq.getZwdm());
    }

    /**
     * 获取历史职位分数线
     *
     * @param examScoreLineReq
     * @return
     */
    @Override
    public List<HistoryScoreLineVo> getHistoryPostScoreLine(ExamScoreLineReq examScoreLineReq) {
        List<HistoryScoreLineVo> historyScoreLineVos = new ArrayList();
        //获得最近5年的年份
        List<String> yearList = null;
        try {
            yearList = DateUtils.getLatestFiveYears(null);
        } catch (Exception e) {
            throw new CustomException(BizErrorCode.COM_E0004);
        }
        for (String year : yearList) {
            HistoryScoreLineVo historyScoreLineVo = servantExamineeInfoMapper.getHistoryPostScoreLine(examScoreLineReq, year);
            if (historyScoreLineVo == null) {
                historyScoreLineVo = new HistoryScoreLineVo();
                historyScoreLineVo.setExamYear(year);
            }else
            {
                String scoreLine = historyScoreLineVo.getScoreLine();
                String maxScore = historyScoreLineVo.getMaxScore();
                scoreLine = new BigDecimal(scoreLine).setScale(1,RoundingMode.HALF_UP).toString();
                maxScore = new BigDecimal(maxScore).setScale(1,RoundingMode.HALF_UP).toString();
                historyScoreLineVo.setScoreLine(scoreLine);
                historyScoreLineVo.setMaxScore(maxScore);
            }

            historyScoreLineVos.add(historyScoreLineVo);
        }
        //获取最近五年职位分数线
        return historyScoreLineVos;
    }

    //更新面试人员名单和考试合格字段
    @Override
    public void updateInterViewAndQualifiedList() {
        //获得有考试变更的记录
        List<ExamInfo> examInfoList = examInfoMapper.selectChangedServantExamInfoList();
        if (CollectionUtils.isEmpty(examInfoList)) {
            log.info("暂无考试信息,不执行更新操作");
            return;
        }
        for (ExamInfo examInfo : examInfoList) {
            updateQualifiedList(examInfo.getId());
            updateInterViewList(examInfo.getId());
            //  updateChangedExamInfoTime(examInfo.getId());
        }
    }

    //key-examId  value-zwdm
    public void updateInterViewList(String examId) {
        log.info("开始更新面试人员名单");
        long startTime = System.currentTimeMillis();

        List<String> Zwdms = postInfoMapper.queryZwdmByExamId(examId);
        //循环更新考生信息或分数线有改动的职位
        //考试id和职位代码
        for (String zwdm : Zwdms) {

            //获取进入面试的考生ID
            ServantExamineeInfo examineeInfo = new ServantExamineeInfo();
            examineeInfo.setExamId(examId);
            examineeInfo.setBkzwdm(zwdm);
            examineeInfo.setInterviewStatus("Y");
            List<ServantExamineeInfo> examineeInfoList = servantExamineeInfoMapper.selectServantExamineeInfoList(examineeInfo);
            List<String> interviewVoIds = examineeInfoList.stream().map(ServantExamineeInfo -> ServantExamineeInfo.getId()).collect(Collectors.toList()); // 获取进入面试考生ID

            //重新排名进入面试的考生的id
            log.info("重新排名进入面试的考生");
            List<String> reloadInterviewIds = rankInterview(examId, zwdm);

            List<String> differentId = ListUtil.getDiffrent(interviewVoIds, reloadInterviewIds);
            if (null == differentId || differentId.size() == 0) {
                log.info("该职位不需要更新面试名单");
                continue;
            }

            //根据id获取考生对象
            List<ServantExamineeInfo> updateList = servantExamineeInfoMapper.selectServantExamineeInfoByIds(differentId);
            //修改面试状态
            for (ServantExamineeInfo servantExamineeInfo : updateList) {
                servantExamineeInfo.setUpdateTime(DateUtils.getNowDate());
                if (StringUtils.equals(servantExamineeInfo.getInterviewStatus(), "Y") || servantExamineeInfo.getInterviewStatus() == null) {
                    servantExamineeInfo.setInterviewStatus("N");
                    continue;
                }
                if (StringUtils.equals(servantExamineeInfo.getInterviewStatus(), "N")) {
                    servantExamineeInfo.setInterviewStatus("Y");
                    continue;
                }
            }

            //批量更新
            try {
                servantExamineeInfoMapper.batchUpdateInterviewStatus(updateList);
            } catch (Exception e) {
                log.error(e.getCause().getMessage());
            }
        }
        long endTime = System.currentTimeMillis();
        log.info("更新结束，共耗时{}秒", (endTime - startTime) / 1000);

    }


    public void updateQualifiedList(String examId) {
        log.info("开始更新考试合格字段");
        long startTime = System.currentTimeMillis();
        //获取该考试下职位代码
        List<String> Zwdms = postInfoMapper.queryZwdmByExamId(examId);

        //考试id和职位代码
        for (String zwdm : Zwdms) {
            //更新合格字段
            //获取已有合格考生列表id
            log.info("开始更新合格字段{}", examId);
            ServantExamineeInfo examineeInfo = new ServantExamineeInfo();
            examineeInfo.setExamId(examId);
            examineeInfo.setBkzwdm(zwdm);
            examineeInfo.setQualifiedStatus("Y");
            List<ServantExamineeInfo> examineeInfoList = servantExamineeInfoMapper.selectServantExamineeInfoList(examineeInfo);
            List<String> qualifiedId = examineeInfoList.stream().map(ServantExamineeInfo -> ServantExamineeInfo.getId()).collect(Collectors.toList()); // 获取合格考生ID
            //重新计算的合格考生列表
            List<ServantExamineeInfoVo> servantExamineeInfoVos = getPassExamineeList(examId, zwdm);
            List<String> reloadQualifiedId = servantExamineeInfoVos.stream().map(ServantExamineeInfoVo::getId).collect(Collectors.toList());
            //对比两次获取的考生区别
            List<String> differentIdByQualified = ListUtil.getDiffrent(qualifiedId, reloadQualifiedId);

            if (differentIdByQualified.size() == 0 || differentIdByQualified == null) {
                log.info("该职位不需要更新合格字段");
                continue;
            }

            //获取具体对象
            List<ServantExamineeInfo> updateQualifiedList = servantExamineeInfoMapper.selectServantExamineeInfoByIds(differentIdByQualified);
            //修改合格状态
            for (ServantExamineeInfo servantExamineeInfo : updateQualifiedList) {
                servantExamineeInfo.setUpdateTime(DateUtils.getNowDate());
                if (StringUtils.equals(servantExamineeInfo.getQualifiedStatus(), "Y") || servantExamineeInfo.getQualifiedStatus() == null) {
                    servantExamineeInfo.setQualifiedStatus("N");
                    continue;
                }
                if (StringUtils.equals(servantExamineeInfo.getQualifiedStatus(), "N")) {
                    servantExamineeInfo.setQualifiedStatus("Y");
                    continue;
                }
            }

            //批量更新
            try {
                servantExamineeInfoMapper.batchUpdateQualifiedStatus(updateQualifiedList);
            } catch (Exception e) {
                log.error(e.getCause().getMessage());
            }
        }

        long endTime = System.currentTimeMillis();
        log.info("更新结束，共耗时{}秒", (endTime - startTime) / 1000);

    }

    //排名获得面试id
    public List<String> rankInterview(String examId, String bkzwdm) {
        //获取职位信息
        ExamPostInfo examPostInfo = postInfoMapper.selectPostInfoByExamIdAndZwdm(examId, bkzwdm);
        List<ServantExamineeInfoVo> servantExamineeInfoVos = getPassExamineeList(examId, bkzwdm);

        //已及格考生的Id
        List<String> Ids = servantExamineeInfoVos.stream().map(ServantExamineeInfoVo::getId).collect(Collectors.toList());
        if (Ids.isEmpty())
            return null;
        //获取含复合分数对象
        List<InterviewVo> list = servantExamineeInfoMapper.getCrossLineList(Ids);
        List<InterviewVo> resultList = new ArrayList();
        //报名人数
        //int registNum = servantExamineeInfoMapper.getRegistNum(examId,bkzwdm);
        //拆分比例字符串，根据比例计算面试人数
//        String[] temp = StringUtils.split(examPostInfo.getMsrybl(), ":");
//        BigDecimal top = new BigDecimal(temp[0]);//分子
//        BigDecimal buttom = new BigDecimal(temp[1]);//分母
//        BigDecimal msbl = top.divide(buttom, 4, BigDecimal.ROUND_UP);
//        int interviewNum = new BigDecimal(examPostInfo.getZkrs()).divide(msbl, BigDecimal.ROUND_UP).intValue();
        int interviewNum = examPostInfo.getInterviewNum()== null ? 0 : examPostInfo.getInterviewNum();

        //男女不划分
        if (examPostInfo.getZkrsMan() == null && examPostInfo.getZkrsWoman() == null) {
            getResultList(list, resultList, interviewNum);
        }
        //男女划分
        if (examPostInfo.getZkrsMan() != null && examPostInfo.getZkrsWoman() != null) {
            List<InterviewVo> manList = new ArrayList<>();
            List<InterviewVo> womanList = new ArrayList<>();
            int interviewManNum = interviewNum * examPostInfo.getZkrsMan() / examPostInfo.getZkrs();
            int interviewWomanNum = interviewNum * examPostInfo.getZkrsWoman() / examPostInfo.getZkrs();
            for (InterviewVo interviewVo : list) {
                if (StringUtils.equals(interviewVo.getSex(), "1"))
                    manList.add(interviewVo);
                if (StringUtils.equals(interviewVo.getSex(), "2"))
                    womanList.add(interviewVo);
            }
            getResultList(manList, resultList, interviewManNum);
            getResultList(womanList, resultList, interviewWomanNum);
        }
        //返回id
        List<String> resultIds = resultList.stream().map(InterviewVo::getId).collect(Collectors.toList());
        return resultIds;
    }

    //获得报考职位的合格考生列表
    public List<ServantExamineeInfoVo> getPassExamineeList(String examId, String zwdm) {
        //获取职位信息
        ExamPostInfo examPostInfo = postInfoMapper.selectPostInfoByExamIdAndZwdm(examId, zwdm);
        //获取职位分数线
        List<ExamPostScoreLineSettingSub> scoreLine = settingMapper.getPostScoreLine(examPostInfo);
        if (CollectionUtils.isEmpty(scoreLine)) {
            return null;
        }

        ExamPostScoreLineSettingSub compositeScoreLine = null;      //复合分数线
        String compositeSub = null;
        List<ExamPostScoreLineSettingSub> singleScoreLine = new ArrayList<>();    //单科分数线
        for (ExamPostScoreLineSettingSub examPostScoreLineSettingSub : scoreLine){
            if (examPostScoreLineSettingSub.getIsComposite().equals("1")) {
                compositeScoreLine = examPostScoreLineSettingSub;
                compositeSub = examPostScoreLineSettingSub.getSubjectId();
            } else {
                singleScoreLine.add(examPostScoreLineSettingSub);
            }
        }
        Map<String, String> singleScoreLineMap = null;
        if (null != singleScoreLine && singleScoreLine.size() !=0) {
            singleScoreLineMap = singleScoreLine.stream().collect(Collectors.toMap(ExamPostScoreLineSettingSub::getSubjectId, ExamPostScoreLineSettingSub::getScore));
        }
        //该场考试报考该职位的考生
        ServantExamineeInfoVo servantExamineeInfoVo = new ServantExamineeInfoVo();
        servantExamineeInfoVo.setExamId(examId);
        servantExamineeInfoVo.setBkzwdm(zwdm);
        List<ServantExamineeInfoVo> servantExamineeInfoVos = servantExamineeInfoMapper.selectExamineeGradeList(servantExamineeInfoVo);
        //去除未过分数线
        if (null != compositeSub && !compositeSub.isEmpty()) {
            for (int i = servantExamineeInfoVos.size() - 1; i >= 0; i--) {
                Boolean isQualified = true;
                //用来计算复合成绩
                BigDecimal compositeSubScore = new BigDecimal("0");
                for (ServantExamineeSubjectGradeVo servantExamineeSubjectGradeVo : servantExamineeInfoVos.get(i).getGradeList()) {
                    if (compositeSub.contains(servantExamineeSubjectGradeVo.getSubjectId())) {
                        compositeSubScore = compositeSubScore.add(servantExamineeSubjectGradeVo.getScore());
                        continue;
                    }
                    if (singleScoreLineMap != null) {
                        BigDecimal subjectScoreLine = new BigDecimal(singleScoreLineMap.get(servantExamineeSubjectGradeVo.getSubjectId()));
                        if (servantExamineeSubjectGradeVo.getScore().compareTo(subjectScoreLine) < 0) {
                            isQualified = false;
                            break;
                        }
                    }
                }
                if (compositeSubScore.compareTo(new BigDecimal(compositeScoreLine.getScore())) < 0){
                    isQualified = false;
                }
                if (!isQualified) {
                    servantExamineeInfoVos.remove(i);
                }
            }
        }
        return servantExamineeInfoVos;
    }

    //获得进入面试考生列表
    public void getResultList(List<InterviewVo> initList, List<InterviewVo> resultList, int num) {
        if (initList.size() == 0 || initList == null || num ==0)
            return;
        Iterator<InterviewVo> iterator = initList.listIterator();
        int index = 0;
        while (iterator.hasNext()) {
            InterviewVo interviewVo = iterator.next();
            if (index >= num) {
                if (interviewVo.getScore() .equals(resultList.get(resultList.size() - 1).getScore())) {
                    resultList.add(interviewVo);
                    index++;
                    continue;
                } else {
                    return;
                }
            }
            resultList.add(interviewVo);
            index++;
        }
    }


    /**
     * 更新考试时间
     *
     * @param examId
     */
    private void updateChangedExamInfoTime(String examId) {
        ChangedExamInfo changedExamInfo = new ChangedExamInfo();
        changedExamInfo.setExamId(examId);
        changedExamInfo.setStatisticsEndTime(DateUtils.getNowDate());
        changedExamInfoMapper.updateChangedExamInfo(changedExamInfo);
    }

}
